package Hot100.Medium.BinarySearch;

import java.util.Arrays;

public class LC34 {
    public static void main(String[] args) {
        int[] nums = {5, 7, 7, 8, 8, 10};
        int target = 8;
        LC34 solution = new LC34();
        int[] res = solution.searchRange(nums, target);
        System.out.println(Arrays.toString(res));

    }

    public int[] searchRange(int[] nums, int target){
        int start = lowerBound(nums, target);
        if(start == nums.length || nums[start] != target){
            return new int[] {-1, -1};
        }
        int end = lowerBound(nums, target + 1) - 1;
        return new int[] {start, end};
    }

    private int lowerBound(int[] nums, int target){
        int left = -1;
        int right = nums.length;
        while(left + 1 < right){
            int mid = left + (right - left) / 2;
            if(nums[mid] >= target){
                right = mid;
            } else {
                left = mid;
            }
        }
        return right;
    }
}
